

// 百度地图API功能
//创建Map实例
var map = new BMap.Map("map_container");
//初始化武汉坐标
var point = new BMap.Point(112.43145,30.048043);
var offsetY = 200;
//地图平移缩放控件：默认左上方
map.addControl(new BMap.NavigationControl());
//比例尺控件，默认位于地图左下方，显示地图的比例关系
map.addControl(new BMap.ScaleControl());
map.centerAndZoom(point,12);
//添加鼠标滚动缩放
map.enableScrollWheelZoom(true);
//设置标注的图标

//显示地址信息窗口
var marker = new BMap.Marker(point);
//把标注添加到地图上
map.addOverlay(marker);

//地址解析类
var gc = new BMap.Geocoder();
function showLocationInfo(pt, rs){
    var addComp = rs.addressComponents;

    console.log('经纬度：'+pt.lat+' '+pt.lng);
    $("#add-text1").val(pt.lat+','+pt.lng);
}

//删除标注
function deletePoint(){
    var allOverlay = map.getOverlays();
    for (var i = 0; i < allOverlay.length; i++){
        map.removeOverlay(allOverlay[i]);
    }
}

//根据选择框或输入框获取地址
var _area={
    //keyword:'',
    _areaLists:$(".areaLists"),
    _address:$("#address"),
    unChecked:function(val){
        return (val==''||typeof (val)=='undefined'||val==null||
            val=='省份'||val=='地级市'||val=='市、县级市')?false:true;
    },
    list:function(){
        var _joinArea='';

        for(var i=0;i<this._areaLists.length;i++){
            if(_area.unChecked(this._areaLists[i].value)){
                _joinArea+=this._areaLists[i].value+',';
            }
        }
        if(_area.unChecked(this._address.val())){
            _joinArea+=this._address.val();
        }
        var address = '';
        $.ajax({
            url : '/admin/Common/getaddress?data=' + _joinArea,
            type : 'post',
            dataType : 'json',
            async:false,
            contentType : "application/json; charset=utf-8",
        }).done(function(data) {
            address = data
        });
        return address;
    },
    init:function(){
        //绑定百度地图
        var _this=_area.list();
        //定义local
        var local = new BMap.LocalSearch(map, {
            renderOptions: {
                map: map,
                panel: "results",//结果容器id
                autoViewport: true,   //自动结果标注
                selectFirstResult: true  , //指定到第一个目标
                enableRouteSearchBox: true,
                enableRouteInfo:true
            },
            pageCapacity: 1
        });

        console.log('huanghe222');
        console.log(local)

        local.setSearchCompleteCallback(function (searchResult) {
            console.log('huanghe333');
            console.log(searchResult)
            var poi = searchResult.getPoi(0);
            //document.getElementById("result_").value = poi.point.lng + "," + poi.point.lat;
            map.centerAndZoom(poi.point, 12);
            gc.getLocation(poi.point, function(rs){
                showLocationInfo(poi.point, rs);
            });
        });
        local.search(_this);
    }
}

//全局变量：存储address字符串长度做判断
var _thisLength='';
$("#address").keydown(function(){
    _thisLength=$(this).val().length;
})
$("#address").keyup(function(){
    var _newLength=$(this).val().length;
    var timer=setTimeout(function(){
        if(_thisLength<=_newLength){
            deletePoint();
            _area.init();
        }
        else{
            clearTimeout(timer);
        }
    },1000);
})
$("#address").blur(function(){
    deletePoint();
    _area.init();
})


function getList(e, type) {

    $('.'+type).html('');
    var name = '';
    if(type == 'xian') {
        name = '所有区县';
        $('.town').html('');
        $('.town').append("<option value='0'>所有乡镇</option>");
    } else if(type=='town') {
        name = '所有乡镇';
    }

    if(e.value != 0) {
        $.ajax({
            url : areaurl,
            type : 'post',
            dataType : 'json',
            contentType : "application/json; charset=utf-8",
            data : JSON.stringify({'id':e.value}),
        }).done(function(data) {
            var $item = "<option value='0'>"+name+"</option>";
            for (var i in data){
                $item += "<option  value='"+i+"' '>"+data[i]+"</option>";
            }
            $('.'+type).append($item);
        });
    } else {
        $('.'+type).append("<option value='0'>"+name+"</option>");
    }
    $("#address").val("");
    deletePoint();
    _area.init();
}

$(function() {
    _area.init();
});
